import { createRouter, createWebHistory } from 'vue-router';
import HomePage from '../components/HomePage.vue';
import UserManagement from '../components/UserManagement.vue';
import AdminManagement from '../components/AdminManagement.vue';
import MusicManage from '../components/MusicManage.vue';
import CommentManage from '../components/CommentManage.vue';
import NoteComment from '../components/NoteComment.vue';
import InstrumentManage from '../components/InstrumentManage.vue';
import NotationManage from '../components/NotationManage.vue';
import TheoryknowledgeManage from '../components/TheoryknowledgeManage.vue';
import CultureBackgroundManage from '../components/CultureBackgroundManage.vue';
import CheckManage from '../components/CheckManage.vue';
import Approve from '../components/Approve.vue';
import NotApprove from '../components/NotApprove.vue';
import Sidebar from '../components/Sidebar.vue';  
import Login from '../components/Login.vue';
import AdminLoginpage from '../components/AdminLoginpage.vue';
import UserInformationDetailManage from '../components/UserInformationDetailManage.vue';

const routes = [

  {
    path: '/',
    name: 'Login',
    component: Login,
    meta: { hideSidebarAndHeader: true },
  },
  {
    path: '/homepage',
    name: 'HomePage',
    component: HomePage,
    meta: { requiresAuth: true },
  },
  {
    path: '/account-management',
    name: 'AccountManagement',
    redirect: '/account-management/user-management',
    children: [
      { path: 'user-management', name: 'UserManagement', component: UserManagement },
      { path: 'admin-management', name: 'AdminManagement', component: AdminManagement },
      { path: 'admin-login-page', name: 'AdminLoginpage', component: AdminLoginpage },
    ],
    meta: { requiresAuth: true, requiresAdmin: true },
  },
  {
    path: '/content-management',
    name: 'ContentManagement',
    redirect: '/content-management/music-manage',
    children: [
      { path: 'music-manage', name: 'MusicManage', component: MusicManage },
      { path: 'notation-manage', name: 'NotationManage', component: NotationManage },
      { path: 'instrument-manage', name: 'InstrumentManage', component: InstrumentManage },
      { path: 'theoryknowledge-manage', name: 'TheoryknowledgeManage', component: TheoryknowledgeManage },
      { path: 'culture-background-manage', name: 'CultureBackgroundManage', component: CultureBackgroundManage },
    ],
    meta: { requiresAuth: true, requiresAdmin: true },
  },
  {
    path: '/user-detail/:userId',  // 使用动态路由参数
    name: 'UserInformationDetailManage',
    component: UserInformationDetailManage,
    props: true,  // 允许将路由参数作为 props 传递给组件
  },
  {
    path: '/comment',
    name: 'Comment',
    redirect: '/comment/note-comment',
    children: [
      { path: 'note-comment', name: 'NoteComment', component: NoteComment },
      { path: 'comment-manage', name: 'CommentManage', component: CommentManage },
    ],
    meta: { requiresAuth: true, requiresAdmin: true },
  },
  {
    path: '/notes-management',
    name: 'NotesManagement',
    redirect: '/notes-management/check-manage',
    children: [
      { path: 'check-manage', name: 'CheckManage', component: CheckManage },
      { path: 'approve', name: 'Approve', component: Approve },
      { path: 'not-approve', name: 'NotApprove', component: NotApprove },
    ],
    meta: { requiresAuth: true, requiresAdmin: true },
  },
  {
    path: '/user-details/:userId',
    name: 'UserDetails',
    component: UserInformationDetailManage,
    props: true,
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const admin = JSON.parse(localStorage.getItem('admin'));
  // 如果目标路由需要身份验证且用户未登录，跳转到登录页面
  if (to.meta.requiresAuth && !admin) {
    return next('/');  // 如果没有用户信息，跳转到登录页
  }

  next(); // 如果通过验证，继续访问目标页面
});
export default router;
